﻿// -------------------------------------------------------------------------
//    @FileName         :    NFServiceHero.cpp
//    @Author           :    gaoyi
//    @Date             :    2025/1/15
//    @Email            :    445267987@qq.com
//    @Module           :    NFServiceHero
//
// -------------------------------------------------------------------------

#include "NFServiceHero.h"

#include <NFHeroPart.h>
#include <NFPlayer.h>
#include <NFComm/NFPluginModule/NFCheck.h>

#include "NFHeroAttrCalcFactory.h"
#include "NFPacketSendUtil.h"
#include "NFPlayerUtil.h"
#include "proto_cs_cmd.nanopb.h"

HeroOne* NFServiceHero::GetHero(NFPlayer* pPlayer)
{
    CHECK_NULL_RE_NULL(0, pPlayer, "");
    NFHeroPart* pHerorPart = pPlayer->GetPart<NFHeroPart>(PART_HERO);
    CHECK_NULL_RE_NULL(pPlayer->GetRoleID(), pHerorPart, "");

    return pHerorPart->GetHero();
}

int NFServiceHero::GetHeroJob(NFPlayer* pPlayer)
{
    HeroOne* pHero = GetHero(pPlayer);
    if (pHero)
    {
        return pHero->job;
    }
    return 0;
}

int64_t NFServiceHero::GetAttr(NFPlayer* pPlayer, int attrType)
{
    CHECK_NULL(0, pPlayer);
    NFHeroPart* pHeroPart = pPlayer->GetPart<NFHeroPart>(PART_HERO);
    CHECK_NULL(pPlayer->GetRoleID(), pHeroPart);
    return pHeroPart->GetAttr(attrType);
}

int NFServiceHero::AddAttr(NFPlayer* pPlayer, int attrType, int64_t llValue, int iReason, int param)
{
    CHECK_NULL(0, pPlayer);
    NFHeroPart* pHeroPart = pPlayer->GetPart<NFHeroPart>(PART_HERO);
    CHECK_NULL(pPlayer->GetRoleID(), pHeroPart);
    return pHeroPart->AddAttr(attrType, llValue, iReason, param);
}

int NFServiceHero::SetAttr(NFPlayer* pPlayer, int attrType, int64_t llValue, int iReason, int param)
{
    CHECK_NULL(0, pPlayer);
    NFHeroPart* pHeroPart = pPlayer->GetPart<NFHeroPart>(PART_HERO);
    CHECK_NULL(pPlayer->GetRoleID(), pHeroPart);
    return pHeroPart->SetAttr(attrType, llValue, iReason, param);
}

int NFServiceHero::RecalcHeroAttr(NFPlayer* pPlayer, int iReason)
{
    CHECK_NULL(0, pPlayer);
    NFHeroPart* pHeroPart = pPlayer->GetPart<NFHeroPart>(PART_HERO);
    CHECK_NULL(pPlayer->GetRoleID(), pHeroPart);
    NFHeroAttrCalcFactory::Recalc(pHeroPart->GetHero(), pPlayer);
    pHeroPart->MarkDirty();

    HeroNotify(pPlayer, iReason);
    return 0;
}

int NFServiceHero::HeroNotify(NFPlayer* pPlayer, int iReason)
{
    CHECK_NULL(0, pPlayer);
    NFHeroPart* pHeroPart = pPlayer->GetPart<NFHeroPart>(PART_HERO);
    CHECK_NULL(pPlayer->GetRoleID(), pHeroPart);
    proto_ff::Proto_CSHeroNtf heroNtf;
    pHeroPart->GetHero()->ToPb(heroNtf.mutable_hero());
    heroNtf.set_chg_reason(iReason);

    NFPacketSendUtil::SendToClient(pPlayer, E_CS_HERO_NTF, &heroNtf);
    return 0;
}


